﻿using CRM.Models;
using System.Linq;
using CRM.Extensions;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using CRM.ViewModels.System;
using Microsoft.EntityFrameworkCore;


namespace CRM.Controllers.SystemManage
{
    [Route("api/[controller]")]
    [ApiController]
    public class PointsConsumptionLog : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly UserManager<User> userManager;

        public PointsConsumptionLog(MyDbContext dbContext, UserManager<User> userManager)
        {
            _dbContext = dbContext;
            this.userManager = userManager;
        }
        
        /// <summary>
        /// 获取积分通用配置(Id为1)
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        public async Task<ResultStruct> GetList([FromQuery] int id)
        {
            var result = 
                await  _dbContext.PointsConsumptionLog
                    .Where(m => m.CompanyId == id)
                    .OrderByDescending(m => m.Id)
                    .ToPaginateAsync();
            return Success(result);
        }        
        
        [HttpPost("[action]")]
        public async Task<ResultStruct> UpdatePointsGeneralConfiguration(CompanyPointsConfigVM vm)
        {
            //TODO: 限定特定人员才能调用
            var model = 
                await _dbContext.CompanyPointsConfig
                    .Where(m => m.Id == 1)
                    .FirstAsync();
            vm.UpdateModel(model);
            await _dbContext.SaveChangesAsync();
            return Success();
        }
        
        
    }
}